package com.tanhua.server.controller;

import com.tanhua.model.dto.MovementDto;
import com.tanhua.model.vo.MovementsVo;
import com.tanhua.model.vo.PageResult;
import com.tanhua.model.vo.VisitorsVo;
import com.tanhua.server.service.CommentsService;
import com.tanhua.server.service.MovementsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

/**
 * @author lxb哦吼吼
 * @version 1.0
 * @description: TODO
 * @date 2021/11/15 19:52
 */

@RestController
@RequestMapping("/movements")
public class MovementsController {

    @Autowired
    private MovementsService movementsService;

    @Autowired
    private CommentsService commentsService;

    /**
     * description: 动态发布 <br>
     * version: 1.0 <br>
     * date: 2021/11/15 21:12 <br>
     * author: SangWu <br>
     *
     * @param movementDto
     * @param imageContent
     * @return org.springframework.http.ResponseEntity
     */
    @PostMapping()
    public ResponseEntity movements(MovementDto movementDto, MultipartFile[] imageContent) throws IOException {
        movementsService.publishMovement(movementDto, imageContent);
        return ResponseEntity.ok(null);
    }

    /**
     * description: 我的动态 <br>
     * version: 1.0 <br>
     * date: 2021/11/15 21:12 <br>
     * author: SangWu <br>
     *
     * @param userId
     * @param page
     * @param pagesize
     * @return org.springframework.http.ResponseEntity
     */
    @GetMapping("/all")
    public ResponseEntity findByUserId(Long userId,
                                       @RequestParam(defaultValue = "1") Integer page,
                                       @RequestParam(defaultValue = "10") Integer pagesize) {
        PageResult pr = movementsService.findByUserId(userId, page, pagesize);
        return ResponseEntity.ok(pr);
    }

    /**
     * 好友动态
     *
     * @param page
     * @param pagesize
     * @return
     */
    @GetMapping
    public ResponseEntity friendMovements(@RequestParam(defaultValue = "1") Integer page,
                                          @RequestParam(defaultValue = "10") Integer pagesize) {
        PageResult pr = movementsService.findFriendMovements(page, pagesize);
        return ResponseEntity.ok(pr);
    }

    /**
     * 推荐动态
     *
     * @param page
     * @param pagesize
     * @return
     */
    @GetMapping("/recommend")
    public ResponseEntity recommend(@RequestParam(defaultValue = "1") Integer page,
                                    @RequestParam(defaultValue = "10") Integer pagesize) {
        PageResult pr = movementsService.findRecommendMovements(page, pagesize);
        return ResponseEntity.ok(pr);
    }

    /**
     * 查询单条动态
     *
     * @param movementId
     * @return
     */
    @GetMapping("/{id}")
    public ResponseEntity findById(@PathVariable("id") String movementId) {
        MovementsVo vo = movementsService.findMovementById(movementId);
        return ResponseEntity.ok(vo);
    }

    /**
     * 动态点赞
     *
     * @param movementId
     * @return
     */
    @GetMapping("/{id}/like")
    public ResponseEntity like(@PathVariable("id") String movementId) {
        Integer likeCount = commentsService.likeMovement(movementId);
        return ResponseEntity.ok(likeCount);
    }

    /**
     * 取消点赞
     *
     * @param movementId
     * @return
     */
    @GetMapping("/{id}/dislike")
    public ResponseEntity dislike(@PathVariable("id") String movementId) {
        Integer likeCount = commentsService.dislikeMovement(movementId);
        return ResponseEntity.ok(likeCount);
    }

    /**
     * 动态喜欢
     *
     * @param movementId
     * @return
     */
    @GetMapping("/{id}/love")
    public ResponseEntity love(@PathVariable("id") String movementId) {
        Integer likeCount = commentsService.loveMovement(movementId);
        return ResponseEntity.ok(likeCount);
    }

    /**
     * 取消喜欢
     *
     * @param movementId
     * @return
     */
    @GetMapping("/{id}/unlove")
    public ResponseEntity unlove(@PathVariable("id") String movementId) {
        Integer likeCount = commentsService.disloveMovement(movementId);
        return ResponseEntity.ok(likeCount);
    }

    /**
     * 查询访客列表
     *
     * @return
     */
    @GetMapping("/visitors")
    public ResponseEntity queryVisitorsList() {
        List<VisitorsVo> list = movementsService.queryVisitorsList();
        return ResponseEntity.ok(list);
    }
}
